package com.lagou.es;


import com.lagou.pojo.Product;
import com.lagou.pojo.ProductQuery;
import com.lagou.respository.ProductRepository;
import com.lagou.resultmapper.ESSearchResultMapper;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.util.List;

/**
 * @author yuting.cai
 */
@org.springframework.stereotype.Service
public class EsService {

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;


    public Product upload(Product product){

        Product save = productRepository.save(product);

        return save;



    }


    public List<Product> query(ProductQuery productQuery){
        //1.构架一个原生查询器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //2.source过滤
        //2.1 参数：final String[] includes, final String[] excludes
        //如果不想执行source过滤可以将该行注释
        queryBuilder.withSourceFilter(new FetchSourceFilter(new String[0],new String[0]));
        //3.查询条件
        //queryBuilder.withQuery(QueryBuilders.matchQuery("name",productQuery.getName()));
        //queryBuilder.withQuery(QueryBuilders.rangeQuery("price").gte(productQuery.getMinPrice()).lt(productQuery.getMaxPrice()));
        MatchQueryBuilder queryBuilder1 = QueryBuilders.matchQuery("name", productQuery.getName());
        RangeQueryBuilder queryBuilder2 = QueryBuilders.rangeQuery("price").gte(productQuery.getMinPrice()).lt(productQuery.getMaxPrice());
        BoolQueryBuilder queryBuilder3 = QueryBuilders.boolQuery().must(queryBuilder1).must(queryBuilder2);

        queryBuilder.withQuery(queryBuilder3);

        //4.设置分页和排序规则
        queryBuilder
                .withPageable(PageRequest.of(productQuery.getPage(),5, Sort.by(Sort.Direction.ASC,"price")));

        //5.高亮
        HighlightBuilder.Field field = new HighlightBuilder.Field("name");
        field.preTags("<font style='color:red'>");
        field.postTags("</font>");
        queryBuilder.withHighlightFields(field);
//        //6.聚合
//        queryBuilder.addAggregation(AggregationBuilders.terms("brandAgg").field("brand"));
        //7.查询
        AggregatedPage<Product> result = elasticsearchTemplate.queryForPage(queryBuilder.build(), Product.class,new ESSearchResultMapper());
        //获取结果
        long total = result.getTotalElements();
        //页码
        int totalPages = result.getTotalPages();
        //获得本页的数据集合
        List<Product> content = result.getContent();
        System.out.println(total+"  "+totalPages);
        content.stream().forEach(product -> System.out.println(product));

        return content;
    }
}
